package com.zth.controller;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.tomcat.util.json.JSONParser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.fasterxml.jackson.databind.deser.std.JsonLocationInstantiator;
import com.github.pagehelper.PageInfo;
import com.zth.pojo.Tag;
import com.zth.service.TagService;

@RestController
@RequestMapping("tag")
public class TagController {
	@Autowired
	private TagService tagService;

	@RequestMapping("tagAll")
	public Map<String, Object> tagAll(Tag tag, @RequestParam(defaultValue = "1", name = "page") int page,
			@RequestParam(defaultValue = "50", name = "limit") int pageSize) {
		System.out.println(tag);
		List<Tag> list = tagService.tagAll(tag, page, pageSize);
		PageInfo<Tag> info = new PageInfo<>(list);
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("code", 0);
		map.put("mes", "");
		map.put("count", info.getTotal());
		map.put("list", info.getList());
		return map;
	}

	@RequestMapping("tagByName")
	public List<Integer> tagByName(Tag tag) {
		String tname = tag.getTag_name();
		// 将字符串切割成字符串数组
		String[] spit = tname.split(",");
		/*
		 * for(String ss:spit) { System.out.println(ss); }
		 */
		List<Integer> tag_id = new ArrayList<>();
		int[] tag2 = tagService.tagByName(spit);
		for (int ss : tag2) {
			tag_id.add(ss);
			System.out.println(ss);
		}
		System.out.println(tag_id);
		return tag_id;
	}

	@RequestMapping("tagById")
	public Tag tagById(int tags_id) {
		return tagService.tagById(tags_id);
	}

	@RequestMapping("add")
	public int add(Tag tag) {
		LocalDateTime create_time = LocalDateTime.now();
		tag.setCreate_time(create_time);
		int row = tagService.add(tag);
		return row;
	}

	@RequestMapping("deleteBatch")
	public int deleteBatch(@RequestParam(name = "ids[]") int[] ids) {

		return tagService.deleteBatch(ids);
	}
}
